package com.young.commom.httpclient;

import com.young.common.expression.dns.NamingResolver;

import java.io.Serializable;
import java.util.Map;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/29/24 12:35 PM
 */
public class HttpClientConfiguration implements Serializable {
    public static final int DEFAULT_MAX_CONNECTIONS = 1024;
    public static final long DEFAULT_CONNECTION_TIMEOUT = 500L;
    public static final long DEFAULT_SOCKET_TIMEOUT = 1000L;
    public static final long DEFAULT_KEEP_ALIVE_TIME = 6000L;
    public static final int DEFAULT_HTTP_RETRY_TIMES = 1;
    public static final boolean DEFAULT_HTTP_RETRY_ON_FAILURE = true;
    public static final long DEFAULT_REQUEST_TIMEOUT = 3000L;
    private long connectionTimeout = 500L;
    private long socketTimeout = 1000L;
    private long keepAliveTime = 6000L;
    private int retryTimes = 1;
    private boolean retryOnFailure = true;
    private String charset = "UTF-8";
    private Map<String, String> globalHeaders;
    private long requestTimeout = 3000L;
    private String clientType = "httpComponents";
    private boolean dnsResolverEnabled = false;
    private NamingResolver namingResolver;

    public boolean isDnsResolverEnabled() {
        return this.dnsResolverEnabled;
    }

    public HttpClientConfiguration setDnsResolverEnabled(boolean dnsResolverEnabled) {
        this.dnsResolverEnabled = dnsResolverEnabled;
        return this;
    }

    public NamingResolver getNamingResolver() {
        return this.namingResolver;
    }

    public HttpClientConfiguration setNamingResolver(NamingResolver namingResolver) {
        this.namingResolver = namingResolver;
        return this;
    }

    public void setConnectionTimeout(Long connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
    }

    public void setSocketTimeout(Long socketTimeout) {
        this.socketTimeout = socketTimeout;
    }

    public void setKeepAliveTime(Long keepAliveTime) {
        this.keepAliveTime = keepAliveTime;
    }

    public void setRetryTimes(Integer retryTimes) {
        this.retryTimes = retryTimes;
    }

    public void setRetryOnFailure(Boolean retryOnFailure) {
        this.retryOnFailure = retryOnFailure;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public Long getConnectionTimeout() {
        return this.connectionTimeout;
    }

    public Long getSocketTimeout() {
        return this.socketTimeout;
    }

    public Long getKeepAliveTime() {
        return this.keepAliveTime;
    }

    public Integer getRetryTimes() {
        return this.retryTimes;
    }

    public Boolean getRetryOnFailure() {
        return this.retryOnFailure;
    }

    public String getCharset() {
        return this.charset;
    }

    public Map<String, String> getGlobalHeaders() {
        return this.globalHeaders;
    }

    public void setGlobalHeaders(Map<String, String> globalHeaders) {
        this.globalHeaders = globalHeaders;
    }

    public int getMaxConnections() {
        return 1024;
    }

    public Long getRequestTimeout() {
        return this.requestTimeout;
    }

    public void setRequestTimeout(long requestTimeout) {
        this.requestTimeout = requestTimeout;
    }

    public String getClientType() {
        return this.clientType;
    }

    public void setClientType(String clientType) {
        this.clientType = clientType;
    }

    private HttpClientConfiguration() {
    }

    public static HttpClientConfiguration common(Map<String, String> globalHeaders) {
        HttpClientConfiguration configuration = new HttpClientConfiguration();
        configuration.setCharset("UTF-8");
        configuration.setConnectionTimeout(500L);
        configuration.setSocketTimeout(1000L);
        configuration.setKeepAliveTime(6000L);
        configuration.setRetryOnFailure(true);
        configuration.setRetryTimes(1);
        if (globalHeaders != null) {
            configuration.setGlobalHeaders(globalHeaders);
        }

        configuration.setRequestTimeout(3000L);
        return configuration;
    }

    public static HttpClientConfiguration common() {
        return common((Map)null);
    }
}
